package com.ztf.gui;

import com.ztf.bean.DataTransfer;
import com.ztf.bean.Message;
import com.ztf.bean.User;
import com.ztf.client.Client;

import java.awt.EventQueue;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.*;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.SystemColor;
import java.awt.Color;
import javax.swing.ImageIcon;
import java.awt.Cursor;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

public class ChatRoom extends JFrame {

	private JPanel contentPane;
	public static ChatRoom chatRoom;
	public static User userNow;
	public static List<Message> msg = new ArrayList<>();
	public JList list_1;

	//为当前客户服务的客服
	public static User customerService;
	//正在输入
	public JLabel label7;
	public boolean isInput = true;
	/**
	 * Create the frame.
	 */
	public ChatRoom() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 50, 1000, 700);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		
		//好友列表
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(19, 38, 214, 622);
		contentPane.add(scrollPane);
		JList list = new JList();
		list.setCellRenderer(new ImageCellRender());
		
		//聊天记录
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(245, 38, 749, 534);
		contentPane.add(scrollPane_1);
		list_1 = new JList();
		list_1.setCellRenderer(new MsgCellRender());
		scrollPane_1.setViewportView(list_1);
		
		String file = "client/head/4.jpg";

		
//		int height = 40;
//	    Point p = new Point();
//	    p.setLocation(0, 20 * height);
//	    scrollPane.getViewport().setViewPosition(p);
		
		//滚动到最后
		int height = 40;
	    Point p = new Point();
	    p.setLocation(0, 20 * height);
	    scrollPane_1.getViewport().setViewPosition(p);
		
		JTextArea textArea = new JTextArea();
		textArea.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				//回车
				if(e.getKeyCode()==10) {
					String text = textArea.getText();
					long time = System.currentTimeMillis();
					User from = new User(userNow.getName(),userNow.getHead(),userNow.getIdentity());
					User to = null;
					if (customerService!=null){
						to = new User(customerService.getName(),customerService.getHead(),customerService.getIdentity());
					}
					Message message = new Message(from,to,time,text);
					Client.send(new DataTransfer("msg",message));
					textArea.setText("");
					isInput = false;

				}
			}
		});
		textArea.setBounds(245, 582, 649, 78);
		contentPane.add(textArea);

		textArea.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (isInput && customerService!=null && e.getKeyCode()!=10){
					Client.send(new DataTransfer("inputing",customerService));
					isInput = false;
				}
			}
		});

		
		JButton btnNewButton = new JButton("发送");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.out.println("dddd");
			}
		});
		btnNewButton.setBounds(906, 631, 88, 29);
		contentPane.add(btnNewButton);
		
		JLabel label_1 = new JLabel(userNow.getName());
		label_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

			}
		});
		label_1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		label_1.setHorizontalAlignment(SwingConstants.RIGHT);
		label_1.setForeground(new Color(30, 144, 255));
		label_1.setBounds(741, 10, 128, 16);
		contentPane.add(label_1);
		
		JLabel lblNewLabel_1 = new JLabel("，欢迎您");
		lblNewLabel_1.setBounds(881, 10, 61, 16);
		contentPane.add(lblNewLabel_1);
		
		//个人头像
		ImagePanel imagePanel = new ImagePanel(null);
		imagePanel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

			}
		});
		imagePanel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		imagePanel.setBounds(954, 0, 40, 40);
		BufferedImage head=null;
		try {

			InputStream is = new ByteArrayInputStream(userNow.getHead());
			head = ImageIO.read(is);
			imagePanel.setImage(head);
			contentPane.add(imagePanel);
			
			JButton button = new JButton("退出");
			button.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {

				}
			});
			button.setBounds(19, 5, 98, 29);
			contentPane.add(button);
			contentPane.repaint();
		} catch (Exception e1) {
			e1.printStackTrace();
		}

		label7 = new JLabel("");
		label7.setBounds(200,5,100,29);
		contentPane.add(label7);
	}

}
